package com.example.ximalayademo.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ximalayademo.R;
import com.example.ximalayademo.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class DetailListAdapter extends RecyclerView.Adapter<DetailListAdapter.InnerHolder> {

    private List<Track>  detailData = new ArrayList<>();
    private SimpleDateFormat mUpdateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");
    private String TAG = "DetailListAdapter";
    private ItemClickListener mOnItemClickListener = null;

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setData(List<Track> tracks) {
        detailData.clear();
        detailData.addAll(tracks);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_detail , parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        View itemView = holder.itemView;
        Track track = detailData.get(position);

        TextView orderTv = itemView.findViewById(R.id.order_text);
        TextView titleTv = itemView.findViewById(R.id.detail_item_title);
        //播放量
        TextView playCountTv = itemView.findViewById(R.id.detail_item_play_count);
        TextView durationTv = itemView.findViewById(R.id.detail_item_duration);
        //更新时间
        TextView updateDateTv = itemView.findViewById(R.id.detail_item_update_time);

        orderTv.setText((position+1) +"");
        titleTv.setText(track.getTrackTitle());
        playCountTv.setText(track.getPlayCount() +"");
        //时长
        long durationMil = track.getDuration() * 1000L;
        String duration = mDurationFormat.format(durationMil);
        durationTv.setText(duration);
        LogUtil.d(TAG, duration);
        String time = mUpdateDateFormat.format(track.getUpdatedAt());
        updateDateTv.setText(time);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    LogUtil.d(TAG , detailData.size() +" --------" + position);
                    mOnItemClickListener.onItemClick(detailData , position);
                }
               // Toast.makeText(v.getContext(), "you click "+ position +"...", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return detailData.size();
    }

    public void setOnItemClickListener(ItemClickListener listener ){
        this.mOnItemClickListener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(List<Track> detailData, int position);
    }

}
